local Armature = require("app.public.avatar.Armature")
local BuffView = class("BuffView",function() return cc.Node:create() end)

function BuffView:ctor(params)
	self.effectId = params.effectId
	local effectCfg = params.buffEffectConfig
	self.effectCfg = effectCfg
	local resPath = effectCfg:getDir()
	local armatureName = effectCfg:getArmatureName()
	local displayNode = nil
	if type(resPath)=="string" and resPath:len()>0 and type(armatureName)=="string" and armatureName:len()>0 then
		displayNode = Armature.create({path="armatures/buff/"..resPath,armatureName=armatureName})
		if not displayNode then
			printError("BuffView armature missing %d path:%s armature:%s",effectCfg:getId(),resPath,armatureName)
		end
	end

	self.offsetY = effectCfg:getOffsetY()

	self.displayNode = displayNode
	if displayNode then
		self:addChild(displayNode)
		displayNode:setPositionY(self.offsetY) -- todo
	end

	self.bornAni = effectCfg:getBornAnimation()
    self.idleAni = effectCfg:getIdleAnimation()
    self.affectAni = effectCfg:getAffectAnimation()
    self.deadAni = effectCfg:getDeadAnimation()
    -- self.bindBone = nil
    -- self.offsetX = 0
    -- self.offsetY = 0

    -- print("===BuffView:create:", self.effectCfg:getId())
    local size = self:getContentSize()
    self._height = size.height

	self.flagBuffEffect = true
end

function BuffView:getEffectId()
    return self.effectId
end

function BuffView:start()
	if string.len(self.bornAni) > 0 then
		self.displayNode:playAni({
			name=self.bornAni,fadeInTime=0,
			completeCallback=handler(self,BuffView.playIdle)})
	else 
		self:playIdle()
	end
end


function BuffView:dead(cb)
	-- print("===BuffView:dead:", self.effectCfg:getId())
	local displayNode = self.displayNode
	local deadAni = self.deadAni
	if string.len(deadAni) > 0  and displayNode:hasAnimation(deadAni) then
		displayNode:setVisible(true)
		displayNode:playAni({
			name=deadAni,
			fadeInTime=0,
			completeCallback=cb})
	else 
		cb()
	end
end

function BuffView:playIdle()
	local displayNode = self.displayNode
	local idleAni = self.idleAni
	if displayNode and string.len(idleAni) > 0 then
		displayNode:playAni({name=idleAni,fadeInTime=0})
	else
		if displayNode then
			displayNode:setVisible(false)
		end
	end
end

function BuffView:playDoing()
	local displayNode = self.displayNode
	local affectAni = self.affectAni

	if displayNode and string.len(affectAni) > 0 and displayNode:hasAnimation(affectAni) then
		displayNode:setVisible(true)
		displayNode:playAni({name=affectAni,fadeInTime=0, completeCallback=handler(self,BuffView.playIdle)})
	end
end

function BuffView:playHurt()
	local displayNode = self.displayNode
	local hitAni = self.hitAni
	if displayNode and string.len(hitAni) > 0 and displayNode:hasAnimation(hitAni) then
		displayNode:setVisible(true)
		displayNode:playAni({name=hitAni,fadeInTime=0,completeCallback=handler(self,BuffView.playIdle)})
	end
end

function BuffView:genRandomHitPos()
	local posX,posY = 0,0
	local ret = false
	local displayNode = self.displayNode
	if displayNode and displayNode.getBonePos then
		local ranIdx = math.random(8)
		local boneName = "hitpoint"..ranIdx
		if displayNode:hasBone(boneName) then
			posX,posY = displayNode:getBonePos(boneName)
			ret = true
		end
	end
	return ret,posX,posY
end

function BuffView:setRemoveCallBack(callback)
    self.removeCallBack = callback
end

function BuffView:setParentRemoveCallBack(callback)
    self.parentRemoveCallBack = callback
end

function BuffView:removeSelf(isCleanUp)
    if self.removeCallBack then
        self.removeCallBack()
    end
	self.removeCallBack = nil

    if self.parentRemoveCallBack then
    	self.parentRemoveCallBack()
    end
	self.parentRemoveCallBack = nil
	
	self:stopAllActions()
    -- self:removeFromParent()
	local parent = self:getParent()
    if parent then
        parent:removeChild(self, isCleanUp)
    end
end

function BuffView:face(facing)
	if self.dir ~= facing then
		self.dir = facing
		self:updateDir()
	end
end

function BuffView:updateDir()
    local displayNode = self.displayNode
    if displayNode then
        local scaleX = math.abs(displayNode:getScaleX())
        displayNode:setScaleX(scaleX*-self.dir)
    end
end

function BuffView:dumpInfo()
    print("dump info BuffView: ReferenceCount:",self:getReferenceCount())
    if self.displayNode then
        print("dump info BuffView: displayNode ReferenceCount:",self.displayNode:getReferenceCount())
    end
end

function BuffView:clearAnimation()
	if self.displayNode then
		local animation = self.displayNode:getAnimation()
		if animation then
            animation:reset()
        end
	end
end

return BuffView